#include "FunTrace.h"
#include <e32base.h>
#include "positionrequest.h"

#include <aknglobalnote.h>
static void PrintNoteL(TRefByValue<const TDesC> aFormat, ...)
    {
    VA_LIST list;
    VA_START(list, aFormat);
    TBuf<256> buf;
    buf.FormatList(aFormat, list);
    VA_END(list);
	CAknGlobalNote* globalNote = CAknGlobalNote::NewLC();
	globalNote->ShowNoteL(EAknGlobalInformationNote, buf); 
    CleanupStack::PopAndDestroy(globalNote);
    }

CPositionRequest* CPositionRequest::NewLC()
	{
TFunTrace funTrace(__PRETTY_FUNCTION__);
	CPositionRequest* ret = new (ELeave) CPositionRequest();
	CleanupStack::PushL(ret);
	ret->ConstructL();
	return ret;
	}

CPositionRequest* CPositionRequest::NewL()
{
TFunTrace funTrace(__PRETTY_FUNCTION__);
	CPositionRequest* ret = NewLC();
	CleanupStack::Pop(ret);
	return ret;
}

CPositionRequest::CPositionRequest() : CActive(EPriorityStandard)
	{
	CActiveScheduler::Add(this);
	}

CPositionRequest::~CPositionRequest()
{
TFunTrace funTrace(__PRETTY_FUNCTION__);
	Cancel();
	iPositioner.Close();
	iServer.Close();
}

void CPositionRequest::SetRequestorL(const TDesC& aDesC)
	{
TFunTrace funTrace(__PRETTY_FUNCTION__);
	User::LeaveIfError( iPositioner.SetRequestor( CRequestor::ERequestorService, CRequestor::EFormatApplication , aDesC ) );	
	}

void CPositionRequest::SetUpdateTimeOutL(TTimeIntervalMicroSeconds aUpdateTimeout)
	{
TFunTrace funTrace(__PRETTY_FUNCTION__);
	TPositionUpdateOptions updateOptions;
	User::LeaveIfError( iPositioner.GetUpdateOptions(updateOptions) );
	updateOptions.SetUpdateTimeOut( TTimeIntervalMicroSeconds(aUpdateTimeout) );
	User::LeaveIfError( iPositioner.SetUpdateOptions(updateOptions) );
	}

void CPositionRequest::ConstructL()
{
TFunTrace funTrace(__PRETTY_FUNCTION__);
	User::LeaveIfError(iServer.Connect());
}

void CPositionRequest::SubmitL(MPositionRequestCallback* aTarget)
	{
TFunTrace funTrace(__PRETTY_FUNCTION__);
	iTarget = aTarget;
	iPositioner.NotifyPositionUpdate(iPositionInfo, iStatus);
	SetActive();
	}

void CPositionRequest::OpenL()
	{
TFunTrace funTrace(__PRETTY_FUNCTION__);
	User::LeaveIfError(iPositioner.Open(iServer));	
	}


TBool CPositionRequest::OpenL(TPositionModuleInfo::TTechnologyType aTechnologyType)
	{
TFunTrace funTrace(__PRETTY_FUNCTION__);
	TUint moduleCount;
	if(iServer.GetNumModules(moduleCount) == KErrNone)
		{
		for(TUint i = 0 ; i < moduleCount ; i++)
			{
			TPositionModuleInfo modInfo;
			if(iServer.GetModuleInfoByIndex( i, modInfo ) == KErrNone && modInfo.IsAvailable() && (modInfo.TechnologyType() == aTechnologyType))
				{
				TBuf<256> moduleName;
				modInfo.GetModuleName(moduleName);
				if( iPositioner.Open(iServer,modInfo.ModuleId()) == KErrNone)
					return ETrue;
				}
			}
		}
	return EFalse;
	}

void CPositionRequest::DoCancel()
{
TFunTrace funTrace(__PRETTY_FUNCTION__);
	iPositioner.CancelRequest(EPositionerNotifyPositionUpdate);
}
void CPositionRequest::RunL()
{
TFunTrace funTrace(__PRETTY_FUNCTION__);
	iTarget->MPositionRequestCallbackRunL(this, iStatus, iPositionInfo);
}

RPositioner& CPositionRequest::Positioner()
	{
TFunTrace funTrace(__PRETTY_FUNCTION__);
	return iPositioner;
	}

RPositionServer& CPositionRequest::PositionServer()
	{
TFunTrace funTrace(__PRETTY_FUNCTION__);
	return iServer;	
	}
